package com.cwj.utils;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.cwj.mylibrary.AppVersionNameAndCode;
import com.cwj.mylibrary.ChinaDate;
import com.cwj.mylibrary.ChinaDate2;
import com.cwj.mylibrary.CopyAndPaste;
import com.cwj.mylibrary.LoadingDialog;
import com.cwj.mylibrary.LunarUtils;
import com.cwj.mylibrary.PhoneInformation;
import com.cwj.mylibrary.TimeUtils;
import com.cwj.mylibrary.WaterMarkUtil;
import com.cwj.utils.databinding.ActivityMainBinding;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initView();
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        //水印
        WaterMarkUtil.showWatermarkView(this, "Hello World!");

        String ymd = TimeUtils.dateToString(TimeUtils.getTimeStame(), "yyyy-MM-dd");
        String y = TimeUtils.dateToString(TimeUtils.getTimeStame(), "yyyy");

        String ny, nm, nd;
        try {
            ny = ChinaDate2.solarToLunar(ymd, false).substring(0, 4);
            nm = ChinaDate2.solarToLunar(ymd, false).substring(5, 7);
            nd = ChinaDate2.solarToLunar(ymd, false).substring(8, 10);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        Calendar today = Calendar.getInstance();
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date time = simpleDateFormat.parse(ymd);
            if (time != null) {
                today.setTime(time);
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        ChinaDate lunar = new ChinaDate(today);

        String[] ry = new String[]{"不闰月", "闰正月", "闰二月", "闰三月", "闰四月", "闰五月", "闰六月", "闰七月",
                "闰八月", "闰九月", "闰十月", "闰冬月", "闰腊月"};

        binding.tv.setText("应用版本：" + AppVersionNameAndCode.getVersionName(this) +
                "(" + AppVersionNameAndCode.getVersionCode(this) + ")"//版本名(版本号)
        );

        try {
            binding.tv2.setText("公历转农历：" + lunar + "\n" +//公历转农历:带天干地支和属相
                    //公历转yyyy-MM-dd格式的农历,true为显示闰字
                    "yyyy-MM-dd格式农历：" + ChinaDate2.solarToLunar(ymd, true) + "\n" +
                    //将农历年月日转换为公历年月日:月(若为闰月则传入负数)
                    "农历转公历：" + LunarUtils.getTranslateSolarString(Integer.parseInt(ny), Integer.parseInt(nm),
                    Integer.parseInt(nd)) + "\n" +
                    //判断公历年是否为闰年
                    "公历年是否为闰年：" + (LunarUtils.isLeapYear(Integer.parseInt(y)) ? "闰年" : "平年") + "\n" +
                    //查询农历年的总天数
                    "农历年的总天数：" + LunarUtils.daysInLunarYear(Integer.parseInt(ny)) + "天\n" +
                    //查询农历年闰哪个月1-12,没闰传回0
                    "农历年闰月为：" + ry[LunarUtils.leapMonth(Integer.parseInt(ny))]
            );
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        binding.tv3.setText("手机厂商：" + PhoneInformation.getManufacturer() + "\n" +
                "手机型号：" + PhoneInformation.getModel() + "\n" +
                "屏幕的宽度：" + PhoneInformation.getDeviceWidth(this) + "\n" +
                "屏幕的高度：" + PhoneInformation.getDeviceHeight(this) + "\n" +
                "屏幕的可用宽度：" + PhoneInformation.getDeviceKYWidth(this) + "\n" +
                "屏幕的可用高度：" + PhoneInformation.getDeviceKYHeight(this) + "\n" +
                "状态栏高度：" + PhoneInformation.getStatusBarHeight(this)
        );

        binding.btn1.setOnClickListener(v -> {
//            MarketUtils.getTools().openMarket(MainActivity.this);
//            MarketUtils.getTools().openMarket(MainActivity.this,"com.quark.browser");
//            MarketUtils.getTools().openMarket(MainActivity.this, "com.quark.browser", MarketUtils.PACKAGE_NAME.HUAWEI_PACKAGE_NAME);
//            MarketUtils.getTools().openInstalledApp(MainActivity.this, "com.quark.browser","com.ucpro.MainActivity");

            /*MarketUtils.getTools().openInstalledAppInURL(MainActivity.this,
                    "com.quark.browser", "com.ucpro.MainActivity", "https://www.baidu.com");*/

//            ToastUtil.l(MainActivity.this, "msg");
//            ToastUtil.imageLL(MainActivity.this, "msg", R.drawable.ic_launcher_foreground);

            LoadingDialog loadingDialog = new LoadingDialog(this);
           /* loadingDialog.setMessage("加载中...");
            loadingDialog.show();*/

            loadingDialog.dismiss();
        });

        //复制
        binding.btn2.setOnClickListener(v ->
                CopyAndPaste.copyClieck(MainActivity.this, binding.et.getText().toString())
        );

        //粘贴
        binding.btn3.setOnClickListener(v -> {
            String str = CopyAndPaste.pasteClieck(MainActivity.this);
            if ("".equals(str.trim())) {
                Toast.makeText(this, "粘贴内容为空", Toast.LENGTH_SHORT).show();
            } else {
                binding.et.setText(str);
            }
        });
    }
}